from google.appengine.ext.db import djangoforms
from google.appengine.api import users


from django import forms


from ckeditor.widgets import CKEditor
from content.models import Publication, Issue, Article
from helpers.misc import new_skey

class PublicationForm(djangoforms.ModelForm):
	class Meta:
		model = Publication
		exclude = ('author', 'permissions', 'updated', 's_key', 'status', )

class IssueForm(djangoforms.ModelForm):
	#FIXME: don't use db.Key
        publication = djangoforms.ModelChoiceField(reference_class = Publication,
                empty_label = "Please select a Publication",
                required = False,
                )

	title = forms.CharField(max_length=100)
	number = forms.IntegerField()

	def __init__(self, *args, **kwargs):
		super(IssueForm, self).__init__(*args, **kwargs)
		self.fields["publication"].query = Publication.all().filter('author =', users.get_current_user())

	def create(self, data):
		return Issue(publication = data['publication'], title = data['title'], number = int(data['number'])).save()

class CreateArticleForm(djangoforms.ModelForm):
	#FIXME: don't use db.Key
	issue = djangoforms.ModelChoiceField(reference_class = Issue,
                empty_label = "Please select an Issue",
                required = False,
                widget = forms.SelectMultiple,
                )
	#FIXME: don't use db.Key
	publication = djangoforms.ModelChoiceField(reference_class = Publication,
                empty_label = "Please select a Publication",
                required = False,
                widget = forms.SelectMultiple,
                )

	title = forms.CharField(max_length=100)
	#FIXME: when editing, instance=article show 'tag1 tag2' without ','
	tags = forms.CharField(max_length=100)
	public = forms.BooleanField(required=False)

	def __init__(self, *args, **kwargs):
		super(CreateArticleForm, self).__init__(*args, **kwargs)
		self.fields["publication"].query = Publication.all().filter('author =', users.get_current_user())
		self.fields["issue"].query = Issue.all().filter('status !=', 2).filter('author =', users.get_current_user())

	def create(self, data):
		return Article(parents = data['parents'], title = data['title'], tags = data['tags'], public = data['public']).save()

class EditArticleForm(djangoforms.ModelForm):
	#FIXME: don't use db.Key
	#FIXME: show model's selected object
	issue = djangoforms.ModelChoiceField(reference_class = Issue,
                empty_label = "Please select an Issue",
                required = False,
                widget = forms.SelectMultiple,
                )
	#FIXME: don't use db.Key
	#FIXME: show model's selected object
	publication = djangoforms.ModelChoiceField(reference_class = Publication,
                empty_label = "Please select a Publication",
                required = False,
                widget = forms.SelectMultiple,
                )

	title = forms.CharField(max_length=100)
	#FIXME: when editing, instance=article show 'tag1 tag2' without ','
	tags = forms.CharField(max_length=100)
	public = forms.BooleanField(required=False)

	content = forms.CharField(widget=CKEditor())

	def __init__(self, *args, **kwargs):
		super(EditArticleForm, self).__init__(*args, **kwargs)
		self.fields["publication"].query = Publication.all().filter('author =', users.get_current_user())
		self.fields["issue"].query = Issue.all().filter('status !=', 2).filter('author =', users.get_current_user())

class ImageForm(djangoforms.ModelForm):
	image = forms.FileField()
